<?php
  class VillageService{
  	function getVillageById($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $selectTambon){
			//echo "Is called";
			//print_r($tambonAccessId); exit;
			if(in_array($selectTambon, $tambonAccessId)){	
				$where = array();
				/* Query select vilage in tambon
        $sql = "
                 SELECT
                  pn_degis_contact.aco_addr_moo,
                  COUNT(pn_degis_contact.aco_addr_moo) AS count_population,
                  pn_degis_tumbol.atb_lat,
                  pn_degis_tumbol.atb_lon
                 FROM 
                  samuidemap.pn_degis_tumbol,
                  samuidemap.pn_degis_contact
                WHERE  
                  pn_degis_contact.aco_tumbol_id = $select AND
                  pn_degis_tumbol.atb_id = $select
                GROUP BY 
                  pn_degis_contact.aco_addr_moo
                ORDER BY 
                  pn_degis_contact.aco_addr_moo";
					*/
					
				//Query select vilage & unit in tambon	
        $sql = "
								SELECT 
									pn_degis_contact.aco_tumbol_id, 								
									pn_degis_contact.aco_addr_moo, 
									pn_degis_contact.aco_unit,
									COUNT(pn_degis_contact.aco_addr_moo) AS count_population, 
									pn_degis_tumbol.atb_lat, 
									pn_degis_tumbol.atb_lon 
								FROM 
									samuidemap.pn_degis_tumbol, 
									samuidemap.pn_degis_contact 
								WHERE 
									pn_degis_contact.aco_tumbol_id = $selectTambon AND 
									pn_degis_tumbol.atb_id = $selectTambon 
								GROUP BY pn_degis_contact.aco_addr_moo, pn_degis_contact.aco_unit 
								ORDER BY CAST(pn_degis_contact.aco_addr_moo AS SIGNED INTEGER), CAST(pn_degis_contact.aco_unit AS SIGNED INTEGER)";
               
               
                  					                  
			    //echo $sql."<br><br>";          
          $column = array('tambon_id', 'moo', 'unit', 'count_population', 'lat', 'lon');
          $result = DBUtil::executeSQL($sql);
          $objectArray = DBUtil::marshallObjects ($result, $column);

          //print_r ($objectArray);
					if($objectArray){
						return $objectArray; 
						//echo "LLLL"; exit;
						//$this->setResponse($uid, $provinceIdArray, $districtIdArray, $tambonIdArray, $objectArray, $select);
					}else{
						$this->setResponseNoData();
					}
				}else{
					echo '<?xml version="1.0" encoding="UTF-8"?>
								<datas>
									<data id=0>
										<title>Permission denied , you cannot access this district.</title>
									</data>
								</datas>';    					
				}
  	}
  	
  	function arrayToXML($uid, $provinceAccessId, $districtAccessId, $tambonAccessId, $objectArray, $selectTambon){  	
  	
  	//Sample $nextxml
	  //http://localhost/zk128/index.php?module=VoteDataCenter&type=service&func=place&province=84&district=8404&tambon=840401,840402,840403,840404,840405,840406&uid=2&st=840406&sm=1&su=1
	  
	  	//echo "http://localhost/zk128/index.php?module=VoteDataCenter&type=service&func=place&province=84&district=8404&tambon=840401,840402,840403,840404,840405,840406&uid=2&st=840406&sm=1&su=1";

			$accessArray['province_access'] = implode(",", $provinceAccessId);
			$accessArray['district_access'] = implode(",", $districtAccessId);
			$accessArray['tambon_access'] = implode(",", $tambonAccessId);
      // Gen xml
      $xml = new DOMDocument('1.0', 'UTF-8');
      $xml->formatOutput = true;
      $xml_node_datas = $xml->createElement("datas");
      $xml->appendChild( $xml_node_datas );

      foreach($objectArray as $key => $val){
        $id = $selectTambon.$val['moo'].$val['unit'];
        $villageId = "vl".$id;
        $villageName = "หมู่ ".$val['moo']." หน่วย ".$val['unit'];
          $xml_node_data = $xml->createElement("data");
          $xml_node_datas->appendChild( $xml_node_data );
          $xml_node_data->setAttribute("id", $villageId);         
            $xml_node_title = $xml->createElement("title",$villageName );
            $xml_node_data->appendChild( $xml_node_title );
            $xml_node_population = $xml->createElement("population",$val['count_population']);
            $xml_node_data->appendChild( $xml_node_population );
            $xml_node_latitude = $xml->createElement("latitude",$val['lat']);
            $xml_node_data->appendChild( $xml_node_latitude );
            $xml_node_longitude = $xml->createElement("longitude",$val['lon']);
            $xml_node_data->appendChild( $xml_node_longitude );
            $xml_node_span = $xml->createElement("span","0.2");
            $xml_node_data->appendChild( $xml_node_span );
            $xml_node_infopath = $xml->createElement("infopath","information.xml");
            $xml_node_data->appendChild( $xml_node_infopath );
            $xml_node_reportpath = $xml->createElement("reportpath","http://data.votethailand.com/report_no_data.xml");
            $xml_node_data->appendChild( $xml_node_reportpath );

            $nextxml = "http://".$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']."?module=VoteDataCenter&amp;type=service&amp;func=place&amp;province=".$accessArray['province_access']."&amp;district=".$accessArray['district_access']."&amp;tambon=".$accessArray['tambon_access']."&amp;uid=".$uid."&amp;st=".$selectTambon."&amp;sm=".trim($val['moo'])."&amp;su=".$val['unit'];
            if ($uid == '6' && $key>='2'){
              $xml_node_xmlpath = $xml->createElement("xmlpath","");
            }else{
              $xml_node_xmlpath = $xml->createElement("xmlpath",$nextxml);
            }
            $xml_node_data->appendChild( $xml_node_xmlpath );
            $xml_node_xmlupdate = $xml->createElement("xmlupdate","LocalityData");
            $xml_node_data->appendChild( $xml_node_xmlupdate );
            $xml_node_date = $xml->createElement("date","2010-11-11 20:26:56");
            $xml_node_data->appendChild( $xml_node_date );

      }

      return $xml;
  	}

  	function setResponseNoData(){
  
     echo '<?xml version="1.0" encoding="UTF-8"?>
            <datas>
              <data id="0">
                <title>ไม่มีข้อมูล</title>
                <population>0</population>
                <latitude>0</latitude>
                <longitude>0</longitude>
                <span>1</span>
                <infopath>http://localhost/gthai/information.xml</infopath>
                <reportpath>http://data.votethailand.com/report_no_data.xml</reportpath>
                <xmlpath></xmlpath>
                <xmlupdate>VillageData</xmlupdate>
                <date>2010-11-11 20:26:56</date>
              </data>
            </datas>';
      pnShutDown();
  	}

  	
  	function renderXMLResponse($xmlObject){
      echo $xmlObject->saveXML();
  	}
  		      				        	
  	function saveXMLResponse($xmlDirectoryPath, $xmlFileName, $xmlObject){      
      $xmlObject->save($xmlDirectoryPath.$xmlFileName);	
  	}  
  }
?>